package com.yb.bussiness.type;

import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgType;

public class TypeServiceImpl implements TypeService {

	private IBaseDao<TgType, Long> dao;

	private String FIND_SQL = "SELECT * FROM T_G_Type where 1=1 ";

	public TypeServiceImpl() {
		dao = new BaseDao<TgType, Long>(TgType.class);
	}

	public void delType(Long id) {
		dao.deleteById(id);
	}

	public TgType read(Long id) {
		return dao.read(id);
	}

	public void saveOrUpdate(TgType Type) {
		if (0 == Type.getId())
			dao.saveEntity(Type);
		else
			dao.update(Type);
	}

	public List<TgType> findAll() {
		return dao.findList(FIND_SQL + " ORDER BY CREATETIME");
	}

	public int getCount(Map<String, String> map) {
		try {
		    String sql = "SELECT count(*) FROM T_G_Type where 1=1 ";
			if (map == null || map.size() == 0)
				return 0;
			if (!"".equals(map.get("name")))
			    sql += "  and name like '%" + map.get("name") + "%'";
			if (!"".equals(map.get("categoryId"))&&!"0".equals(map.get("categoryId")))
			    sql += "  and categoryId = '" + map.get("categoryId") + "'";
			List<Object[]> list = dao.findObjectArryList(sql);
			int total = 0;
	        if (list.size() != 0) {
	            total = Integer.parseInt((list.get(0))[0] + "");
		    }
		return total;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}

	public List<TgType> getList(Map<String, String> map, String start,
			String limt) {
	    if (map == null || map.size() == 0)
			return null;
		try {
		    String sql = "SELECT * FROM T_G_Type where 1=1 ";
			if (!"".equals(map.get("name")))
			    sql += "  and name like '%" + map.get("name") + "%'";
			if (!"".equals(map.get("categoryId"))&&!"0".equals(map.get("categoryId")))
			    sql += "  and categoryId = '" + map.get("categoryId") + "'";
			if (start != null && limt != null) {
	            int snum = Integer.parseInt(start) - 1;
	            sql += " order by code desc, id desc   limit  "
	                    + (snum * Integer.parseInt(limt)) + "," + limt + " ";
	        }
			List<TgType> list = dao.findList(sql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean deleteById(String id) {
		try {
			if ("".equals(id))
				return false;
			String sql = "delete from t_g_Type where id=? ";
			dao.delete(sql, new Object[] { id });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deleteByIdString(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from t_g_Type where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public TgType getById(String id) {
		if ("".equals(id))
			return null;
		String sql = FIND_SQL + " and id=? ";
		try {
			List<TgType> list = dao.findList(sql, new Object[] { id });
			if (null != list && list.size() > 0) {
				TgType tp = list.get(0);
				return tp;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<TgType> findByCategoryId(Long id) {
		String sql = "select * from t_g_type where categoryId = ? order by code desc, id desc";
		return dao.findList(sql, new Object[] { id });
	}

	public boolean checkDel(Long id) {
		boolean flag = true;
		String sql = "select count(*) from t_brand_type where typeId = ?";
		if (bulidResult(sql,new Object[]{id}) > 0)
			flag = false;
		return flag;
	}
	
	private int bulidResult(String sql, Object[] conditionAry) {
		int count = 0;
		List<Object[]> list = dao.findObjectArryList(sql,conditionAry);
		Object[] objAry = list.get(0);
		count = new Integer(objAry[0].toString());
		return count;
	}
}
